package com.atguigu.yygh.hosp.controller.api;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.helper.HttpRequestHelper;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.result.ResultCodeEnum;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.service.HosptialService;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags = "医院管理")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired  //一个注释只能注入一个资源，多注入需要对应多注释
    private HosptialService hosptialService;
    @Autowired
    private HospitalSetService hospitalSetService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ScheduleService scheduleService;

    //删除排班接口
    @ApiOperation(value = "删除排班")
    @PostMapping("schedule/remove")
    public Result remove(HttpServletRequest request){
        //获取排班信息,先转换一下map的类型，方便下一步操作
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //医院编号和排班编号
        String hoscode = (String)paramMap.get("hoscode");
        String hosScheduleId = (String)paramMap.get("hosScheduleId");

        //1 获取医院系统传过来的签名,签名是MD5加密的，map里get出来的是Object类型，需要强转成String
        String hospSign = (String) paramMap.get("sign");
        //2 根据传过来的医院编码，查询此编码对应医院的签名
        //String hoscode = (String)paramMap.get("hoscode");
        System.out.println(hoscode);
        //3 调方法查询(这里直接报了空指针异常，说明根据hoscode查询根本没有执行)
        String signKey = hospitalSetService.getSignKey(hoscode);
        //4 进行MD5加密，加密后再做对比
        String encrypt = MD5.encrypt(signKey);
        //5 判断是否一致
        /*if(!hospSign.equals(encrypt)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }*/
        scheduleService.remove(hoscode,hosScheduleId);
        return Result.ok();
    }

    //查询排班接口
    @ApiOperation(value = "查询排班")
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request){
        //获取排班信息,先转换一下map的类型，方便下一步操作
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //获取医院编号,当前页，每页记录数
        String hoscode = (String)paramMap.get("hoscode");
        //获取科室编号,当前页，每页记录数
        String depcode = (String)paramMap.get("depcode");
        int page = StringUtils.isEmpty(paramMap.get("page"))?1:Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit"))?1:Integer.parseInt((String)paramMap.get("limit"));

        /*//1 获取医院系统传过来的签名,签名是MD5加密的，map里get出来的是Object类型，需要强转成String
        String hospSign = (String) paramMap.get("sign");
        //2 根据传过来的医院编码，查询此编码对应医院的签名
        //String hoscode = (String)paramMap.get("hoscode");
        System.out.println(hoscode);
        //3 调方法查询(这里直接报了空指针异常，说明根据hoscode查询根本没有执行)
        String signKey = hospitalSetService.getSignKey(hoscode);
        //4 进行MD5加密，加密后再做对比
        String encrypt = MD5.encrypt(signKey);
        //5 判断是否一致
        //不知道为什么，医院系统传过来的排班方法里签名就是错的
        if(!hospSign.equals(encrypt)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }*/
        //分页查询，条件封装成VO类
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        //manage系统中好像并没有传输科室编号信息过来
        scheduleQueryVo.setDepcode(depcode);
        //查询
        Page<Schedule> schedulePage = scheduleService.findPageSchedule(page,limit,scheduleQueryVo);
        return Result.ok(schedulePage);
    }

    //上传排班接口
    @ApiOperation(value = "上传排班")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        //获取排班信息,先转换一下map的类型，方便下一步操作
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        String hoscode = (String)paramMap.get("hoscode");
        //1 获取医院系统传过来的签名,签名是MD5加密的，map里get出来的是Object类型，需要强转成String
        String hospSign = (String) paramMap.get("sign");
        //2 根据传过来的医院编码，查询此编码对应医院的签名
        //String hoscode = (String)paramMap.get("hoscode");
        System.out.println(hoscode);
        //3 调方法查询(这里直接报了空指针异常，说明根据hoscode查询根本没有执行)
        String signKey = hospitalSetService.getSignKey(hoscode);
        //4 进行MD5加密，加密后再做对比
        String encrypt = MD5.encrypt(signKey);
        //5 判断是否一致
        /*if(!hospSign.equals(encrypt)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }*/
        scheduleService.save(paramMap);
        return Result.ok();
    }

    //删除科室接口
    @ApiOperation(value = "删除科室")
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request){
        //获取科室信息,先转换一下map的类型，方便下一步操作
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //医院编号和科室编号
        String hoscode = (String)paramMap.get("hoscode");
        String depcode = (String)paramMap.get("depcode");

        //1 获取医院系统传过来的签名,签名是MD5加密的，map里get出来的是Object类型，需要强转成String
        String hospSign = (String) paramMap.get("sign");
        //2 根据传过来的医院编码，查询此编码对应医院的签名
        //String hoscode = (String)paramMap.get("hoscode");
        System.out.println(hoscode);
        //3 调方法查询(这里直接报了空指针异常，说明根据hoscode查询根本没有执行)
        String signKey = hospitalSetService.getSignKey(hoscode);
        //4 进行MD5加密，加密后再做对比
        String encrypt = MD5.encrypt(signKey);
        //5 判断是否一致
        /*if(!hospSign.equals(encrypt)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }*/
        departmentService.remove(hoscode,depcode);
        return Result.ok();
    }

    //查询科室接口,查询中涉及的service操作不懂，要多看看
    @ApiOperation(value = "查询科室")
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request){
        //获取科室信息,先转换一下map的类型，方便下一步操作
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //获取医院编号,当前页，每页记录数
        String hoscode = (String)paramMap.get("hoscode");
        int page = StringUtils.isEmpty(paramMap.get("page"))?1:Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit"))?1:Integer.parseInt((String)paramMap.get("limit"));

        //1 获取医院系统传过来的签名,签名是MD5加密的，map里get出来的是Object类型，需要强转成String
        String hospSign = (String) paramMap.get("sign");
        //2 根据传过来的医院编码，查询此编码对应医院的签名
        //String hoscode = (String)paramMap.get("hoscode");
        System.out.println(hoscode);
        //3 调方法查询(这里直接报了空指针异常，说明根据hoscode查询根本没有执行)
        String signKey = hospitalSetService.getSignKey(hoscode);
        //4 进行MD5加密，加密后再做对比
        String encrypt = MD5.encrypt(signKey);
        //5 判断是否一致
        if(!hospSign.equals(encrypt)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //分页查询，条件封装成VO类
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        //查询
        Page<Department> departmentPage = departmentService.findPageDepartment(page,limit,departmentQueryVo);
        return Result.ok(departmentPage);
    }

    //上传科室
    @ApiOperation(value = "上传科室")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        //获取科室信息,先转换一下map的类型，方便下一步操作
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院编号
        String hoscode = (String)paramMap.get("hoscode");

        //1 获取医院系统传过来的签名,签名是MD5加密的，map里get出来的是Object类型，需要强转成String
        String hospSign = (String) paramMap.get("sign");
        //2 根据传过来的医院编码，查询此编码对应医院的签名
        //String hoscode = (String)paramMap.get("hoscode");
        System.out.println(hoscode);
        //3 调方法查询(这里直接报了空指针异常，说明根据hoscode查询根本没有执行)
        String signKey = hospitalSetService.getSignKey(hoscode);
        //4 进行MD5加密，加密后再做对比
        String encrypt = MD5.encrypt(signKey);
        //5 判断是否一致
        if(!hospSign.equals(encrypt)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用上传科室方法
        departmentService.save(paramMap);
        return Result.ok();
    }

    //查询医院接口
    @ApiOperation(value = "查询医院")
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request){
        //获取医院信息,先转换一下map的类型，方便下一步操作
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院编号
        String hoscode = (String)paramMap.get("hoscode");

        //1 获取医院系统传过来的签名,签名是MD5加密的，map里get出来的是Object类型，需要强转成String
        String hospSign = (String) paramMap.get("sign");
        //2 根据传过来的医院编码，查询此编码对应医院的签名
        //String hoscode = (String)paramMap.get("hoscode");
        System.out.println(hoscode);
        //3 调方法查询(这里直接报了空指针异常，说明根据hoscode查询根本没有执行)
        String signKey = hospitalSetService.getSignKey(hoscode);
        //4 进行MD5加密，加密后再做对比
        String encrypt = MD5.encrypt(signKey);
        //5 判断是否一致
        if(!hospSign.equals(encrypt)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //根据医院编号进行查询
        Hospital hospital = hosptialService.getByHoscode(hoscode);
        return Result.ok(hospital);

    }

    //上传医院接口
    @ApiOperation(value = "上传医院")
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request){

        //获取医院信息,先转换一下map的类型，方便下一步操作
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //1 获取医院系统传过来的签名,签名是MD5加密的，map里get出来的是Object类型，需要强转成String
        String hospSign = (String) paramMap.get("sign");
        //2 根据传过来的医院编码，查询此编码对应医院的签名
        String hoscode = (String)paramMap.get("hoscode");
        System.out.println(hoscode);
        //3 调方法查询(这里直接报了空指针异常，说明根据hoscode查询根本没有执行)
        String signKey = hospitalSetService.getSignKey(hoscode);
        //4 进行MD5加密，加密后再做对比
        String encrypt = MD5.encrypt(signKey);
        //5 判断是否一致
        if(!hospSign.equals(encrypt)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoData = (String)paramMap.get("logoData");
        logoData = logoData.replaceAll(" ","+");
        paramMap.put("logoData",logoData);

        //调用service方法
        hosptialService.save(paramMap);
        return Result.ok();
    }
}
